package com.ruoyi.safe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.safe.domain.TAppUser;
import com.ruoyi.safe.mapper.TAppUserMapper;
import com.ruoyi.safe.service.ITAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * 客户端用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-22
 */
@Service
public class TAppUserServiceImpl extends ServiceImpl<TAppUserMapper, AppUser> implements ITAppUserService {
    @Autowired
    private TAppUserMapper tAppUserMapper;

    /**
     * 查询客户端用户信息
     *
     * @param userId 客户端用户信息主键
     * @return 客户端用户信息
     */
    @Override
    public TAppUser selectTAppUserByUserId(Long userId) {
        return tAppUserMapper.selectTAppUserByUserId(userId);
    }

    /**
     * 查询客户端用户信息列表
     *
     * @param tAppUser 客户端用户信息
     * @return 客户端用户信息
     */
    @Override
    public List<TAppUser> selectTAppUserList(TAppUser tAppUser) {
        return tAppUserMapper.selectTAppUserList(tAppUser);
    }

    /**
     * 新增客户端用户信息
     *
     * @param tAppUser 客户端用户信息
     * @return 结果
     */
    @Override
    public int insertTAppUser(TAppUser tAppUser) {
//        tAppUser.setSalt(generateSalt(8));
//        tAppUser.setPassword(DigestUtils.md5DigestAsHex(tAppUser.getPassword().concat(tAppUser.getSalt()).getBytes()));
        tAppUser.setCreateTime(DateUtils.getNowDate());
        return tAppUserMapper.insertTAppUser(tAppUser);
    }

    public static String generateSalt(int length) {
        // 定义字符集（大小写字母）
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        StringBuilder sb = new StringBuilder(length);

        // 创建 Random 实例
        Random random = new Random();

        // 生成随机字符串
        for (int i = 0; i < length; i++) {
            // 随机选择一个字符
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }

        return sb.toString();
    }

    /**
     * 修改客户端用户信息
     *
     * @param tAppUser 客户端用户信息
     * @return 结果
     */
    @Override
    public int updateTAppUser(TAppUser tAppUser) {
//        tAppUser.setPassword(DigestUtils.md5DigestAsHex(tAppUser.getPassword().concat(tAppUser.getSalt()).getBytes()));
        tAppUser.setUpdateTime(DateUtils.getNowDate());
        return tAppUserMapper.updateTAppUser(tAppUser);
    }

    /**
     * 批量删除客户端用户信息
     *
     * @param userIds 需要删除的客户端用户信息主键
     * @return 结果
     */
    @Override
    public int deleteTAppUserByUserIds(Long[] userIds) {
        return tAppUserMapper.deleteTAppUserByUserIds(userIds);
    }

    /**
     * 删除客户端用户信息信息
     *
     * @param userId 客户端用户信息主键
     * @return 结果
     */
    @Override
    public int deleteTAppUserByUserId(Long userId) {
        return tAppUserMapper.deleteTAppUserByUserId(userId);
    }
}
